package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.dto.request.GradeDto;
import com.huaxiafinance.management.dto.request.MemberGrowthDto;
import com.huaxiafinance.management.dto.request.MemberScoreGradeDto;
import com.huaxiafinance.management.mapper.MemberGradeMapper;
import com.huaxiafinance.management.mapper.MemberGrowthBaseMapper;
import com.huaxiafinance.management.model.MemberGrade;
import com.huaxiafinance.management.model.MemberGrowthBase;
import com.huaxiafinance.management.service.MemberGradeService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("memberGradeService")
@Transactional
public class MemberGradeServiceImpl implements MemberGradeService {

	private static final Logger log = Logger.getLogger(MemberGradeServiceImpl.class);

	@SuppressWarnings("SpringJavaAutowiringInspection")
	@Autowired
	private MemberGrowthBaseMapper memberGrowthBaseMapper;
	@SuppressWarnings("SpringJavaAutowiringInspection")
	@Autowired
	private MemberGradeMapper memberGradeMapper;

	/**
	 * 请求成长倍数值
	 * @return 成长倍数值
	 */
	@Override
	public MessageResult  queryMemberGrowthBase() throws Exception {
		MessageResult messageResult = new MessageResult();
        MemberGrowthBase memberGrowthBase = memberGrowthBaseMapper.query();
        messageResult.setData(memberGrowthBase);
        messageResult.setSuccessResult();
		return messageResult;
	}
	/**
	 * 修改积分成长倍数
	 * @param dto 修改积分成长倍数请求值
	 * @return 返回修改成功与否
	 */
	@Override
	public MessageResult updateMemberGrowthBase(MemberGrowthDto dto) throws Exception {
		MessageResult messageResult = new MessageResult();
		MemberGrowthBase memberGrowthBase = new MemberGrowthBase();
		if (dto.getBaseValue()<0.1){
			messageResult.setFailureResult("最小为0.1");
			return messageResult;
		}
		if (dto.getId()==null){
			messageResult.setFailureResult("请重新点击");
			return messageResult;
		}
		if (dto.getBaseValue()==null){
			messageResult.setFailureResult("请重新点击");
			return messageResult;
		}
		memberGrowthBase.setId(dto.getId());
		memberGrowthBase.setBaseValue(dto.getBaseValue());
		memberGrowthBase.setGmtModified(new Date());
		int n =memberGrowthBaseMapper.updateByPrimaryKeySelective(memberGrowthBase);
		if (n<1){
			messageResult.setFailureResult("请重新点击");
			return messageResult;
		}else {
			messageResult.setSuccessResult();
		}
		return messageResult;
	}
	/**
	 * 查询会员成长倍数列表
	 * @return 会员成长倍数
	 */
	@Override
	public MessageResult  queryMemberGrade() throws Exception {
		MessageResult messageResult = new MessageResult();
		List<MemberGrade> memberGradeList = memberGradeMapper.query();
		messageResult.setData(memberGradeList);
		messageResult.setSuccessResult();
		return messageResult;
	}
	/**
	 * 修改会员成长倍数列表
	 * @param dto 请求参数
	 * @return 返回修改成功否
	 */
	@Override
	public MessageResult updateMemberGrade(GradeDto dto) throws Exception {
		MessageResult messageResult = new MessageResult();
		List<MemberScoreGradeDto> memberGradeList = dto.getMemberGrade();
		for (MemberScoreGradeDto memberScoreGradeDto:memberGradeList){
			MemberGrade memberGrade = new MemberGrade();
			memberGrade.setId(memberScoreGradeDto.getId());
			memberGrade.setStartValue(memberScoreGradeDto.getStartValue());
			memberGrade.setEndValue(memberScoreGradeDto.getEndValue());
			memberGrade.setGradeId(memberScoreGradeDto.getGradeId());
			memberGrade.setGmtModified(new Date());
			int n = memberGradeMapper.updateByPrimaryKeySelective(memberGrade);
            if (n<1){
				messageResult.setFailureResult("请重新点击");
				return messageResult;
			}
		}
		messageResult.setSuccessResult();
		return messageResult;
	}

}
